package com.uenpay.im.ui.old.activity.attendance;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-3-16 下午6:59:17
 * @copyRight http://www.qiyunxin.com
 * @function 签到列表
 */
public class AttendanceListInvokeItem extends BaseHttpInvokeItem {

	public class AttendanceListInvokeItemResult extends HttpInvokeResult {
		public ArrayList<AttendanceEntity> arrayList = new ArrayList<AttendanceEntity>();
	};

	public AttendanceListInvokeItemResult getOutPut() {
		return (AttendanceListInvokeItemResult) GetResultObject();
	}

	/**
	 * 
	 * @param entid
	 * @param custids
	 * @param startdate
	 * @param enddate
	 * @param page
	 */
	public AttendanceListInvokeItem(String entid, String custids,
			String startdate, String enddate, int page) {
		String url = APIConfiguration.getBaseUrl()
				+ "Oa/Attendance/getAttendanceList?entid=" + entid
				+ "&custids=" + custids + "&enddate=" + enddate + "&startdate="
				+ startdate + "&pageIndex=" + page + "&pageSize=12&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		AttendanceListInvokeItemResult result = new AttendanceListInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject dataJson = json.optJSONObject("data");
			if (dataJson == null) {
				return result;
			}
			JSONArray arr = dataJson.optJSONArray("data");
			if (arr == null) {
				return result;
			}
			for (int i = 0, size = arr.length(); i < size; i++) {
				result.arrayList.add(desAttendanceEntity(arr.optJSONObject(i)));
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

	private AttendanceEntity desAttendanceEntity(JSONObject json) {
		AttendanceEntity mAttendanceEntity = new AttendanceEntity();
		mAttendanceEntity.abnormal = json.optInt("abnormal");
		mAttendanceEntity.id = json.optInt("id");
		mAttendanceEntity.entid = json.optInt("entid");
		mAttendanceEntity.checkindate = json.optString("checkindate");
		mAttendanceEntity.custid = json.optString("custid");
		mAttendanceEntity.checkintime = json.optLong("checkintime");
		mAttendanceEntity.checkouttime = json.optLong("checkouttime");
		mAttendanceEntity.timetablestart = json.optString("timetablestart");
		mAttendanceEntity.timetableend = json.optString("timetableend");
		return mAttendanceEntity;
	}
}
